package com.redsoft.spirit.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
   
/**
 * Http工具类.
 * @author arnold
 *
 */
public class HttpUtil {

    /**
     * 构造方法.
     */
    private HttpUtil() {}
    
	/**
	 * 向响应里写as_Msg信息.
	 * 默认使用"text/html","UTF-8".
	 * @param response -- 响应
	 * @param as_Msg -- 要输出的信息
	 * @see #write(HttpServletResponse, String, String, String)
	 */
	public static void write(HttpServletResponse response,String as_Msg){
		write(response,as_Msg,"text/html","UTF-8");
	}
	
	/**
	 * 向响应里写as_Msg信息，并可以指定数据类型及编码类型.
	 * 数据类型默认为："text/html"
	 * 编码方式默认为："UTF-8
	 * @param response  -- 响应
	 * @param as_Msg -- 要输出的信息
	 * @param as_ContentType  -- 数据类型
	 * @param as_Charset  -- 编码方式
	 */
	public static void write(HttpServletResponse response,String as_Msg,String as_ContentType,String as_Charset){
		as_ContentType =  CommonUtil.trim(as_ContentType);
		if(as_ContentType.length() == 0)as_ContentType = "text/html";
		as_Charset = CommonUtil.trim(as_Charset);
		if(as_Charset.length() == 0) as_Charset = "UTF-8";
		
		response.setContentType(as_ContentType + "; charset=" + as_Charset);
	    PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write(as_Msg);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) writer.close();
		}
	}
	
	/**
	 * 获取request所代表的URL(不包括http://[ip/server]/web_context部分).
	 * @param request
	 * @return
	 */
	public static String getUrl(HttpServletRequest request) { 
	    if (request == null) {
	        printMsg("getUrl()","request is null");
	        return null;
	    }
	    
	    //在Tomcat6的环境下和Tomcat5的环境下有所不同
	    //tomcat5:
        //String url = new String(request.getRequestURL());
        //tomcat6:
	    String url = "";
	    if (request.getAttribute("javax.servlet.forward.request_uri") != null) {
	    	url = CommonUtil.trim((String)request.getAttribute("javax.servlet.forward.request_uri"));
	    }
	    if (url.length() == 0) url = new String(request.getRequestURL());
	    
	    //去掉http://[ip/server]/web_context部分
        String webContext = CommonUtil.trim(request.getContextPath());
        int startIndex = url.indexOf(webContext);
        url = url.substring(startIndex + webContext.length());
        
        String queryString = CommonUtil.trim(request.getQueryString());
        if (queryString.length() > 0) url += "?" + queryString;
        
        return url;
    }
	
	/**
    * 访问指定的URL，并返回该URL的内容字符串.
    * @param url 指定的URL
    * @return 内容字符串
    */
    public static String accessURL(String url) {  
        return accessURL(url, "");
    }
	
    /**
     * 访问指定的URL，并返回该URL的内容字符串.
     * @param url 指定的URL
     * @param charSet 处理返回的内容时所使用的字符集.
     * @return 内容字符串
     */
     public static String accessURL(String url, String charSet) {  
         String returnStr = "";
         try {
             java.net.URL lurl_URL = new java.net.URL(url);
             InputStream inputStream = lurl_URL.openStream();
             
             returnStr = CommonUtil.getInputStreamAsString(inputStream, charSet);                 
             inputStream.close();
             
         } catch (Exception e) {
             printMsg("accessURL()" ,e.getMessage() + " 请检查相应URL是否有效.");
         }
  
         return returnStr;
      }
     
	/**
     * 通过System.out.println()的方式在控制台显示相应提示信息.
     * @param as_MethodName 提示信息所在的方法名
     * @param as_Msg 提示信息内容
     */
    private static void printMsg(String as_MethodName, String as_Msg) {
        System.out.println(HttpUtil.class.getName() + "." + as_MethodName + " : " + as_Msg);
    }
}
